package com.example.util;

import com.example.common.util.KeyValueDto;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by James.H.Fu on 2017/5/27.
 */
public class EnumUtils {

    public static <E extends Enum<?> & com.example.util.BaseEnum> E fromValue(Class<E> enumClass, int value) {
        E[] enumConstants = enumClass.getEnumConstants();
        for (E e : enumConstants) {
            if (e.getValue() == value) {
                return e;
            }
        }
        return null;
    }

    public static <E extends Enum<?> & com.example.util.BaseEnum> E fromValue(Class<E> enumClass, String value) {
        E[] enumConstants = enumClass.getEnumConstants();
        for (E e : enumConstants) {
            if (e.getDescription().equals(value)) {
                return e;
            }
        }
        return null;
    }

    public static <E extends com.example.util.BaseEnum> List<KeyValueDto> getAllDescription(List<E> list) {
        List<KeyValueDto> result = new ArrayList<>(list.size());
        for (com.example.util.BaseEnum be : list) {
            result.add(new KeyValueDto(be.getValue(), be.getDescription()));
        }
        return result;
    }
}
